package com.ptmall.app.utils;

import android.graphics.Bitmap;
import android.os.Environment;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.DecimalFormat;

public class FileUtils {
	
	public static String SDPATH = Environment.getExternalStorageDirectory()
			+ "/Photo_LJ/";
	/**
	 * 创建文件夹
	 */
	public static void CreateFile(){
		File dirFirstFolder = new File(FileUtils.SDPATH);
		if(!dirFirstFolder.exists())
		{ //如果该文件夹不存在，则进行创建
			dirFirstFolder.mkdirs();//创建文件夹

		}
	}
	/**
	 * 保存图片
	 * @param bm
	 * @param picName
     */
	public static void saveBitmap(Bitmap bm, String picName) {
		try {
			if (!isFileExist("")) {
				File tempf = createSDDir("");
			}
			File f = new File(SDPATH, picName + ".JPEG"); 
			if (f.exists()) {
				f.delete();
			}
			FileOutputStream out = new FileOutputStream(f);
			bm.compress(Bitmap.CompressFormat.JPEG, 90, out);
			out.flush();
			out.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

    /**
     * 无压缩保存图片 并返回保存位置
     * @return
     */
    public static String saveBitmap(Bitmap bm) {
        String savePath = null;
        try {
            if (!isFileExist("")) {
                File tempf = createSDDir("");
            }
            String fileName = String.valueOf(System.currentTimeMillis());
            File f = new File(SDPATH, fileName + ".JPEG");
            if (f.exists()) {
                f.delete();
            }
            FileOutputStream out = new FileOutputStream(f);
            bm.compress(Bitmap.CompressFormat.JPEG, 100, out);
            out.flush();
            out.close();
            savePath = f.getAbsolutePath();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return savePath;
    }

	/**
	 * 创建SD目录
	 * @param dirName
	 * @return
	 * @throws IOException
     */
	public static File createSDDir(String dirName) throws IOException {
		File dir = new File(SDPATH + dirName);
		if (Environment.getExternalStorageState().equals(
				Environment.MEDIA_MOUNTED)) {

			System.out.println("createSDDir:" + dir.getAbsolutePath());
			System.out.println("createSDDir:" + dir.mkdir());
		}
		return dir;
	}


	public static boolean isFileExist(String fileName) {
		File file = new File(SDPATH + fileName);
		file.isFile();
		return file.exists();
	}
	/**
	 * 文件是存在的
	 * @param path
	 * @return
	 */
	public static boolean fileIsExists(String path) {
		try {
			File f = new File(path);
			if (!f.exists()) {
				return false;
			}
		} catch (Exception e) {

			return false;
		}
		return true;
	}
	/**
	 * 删除文件
	 * @param fileName
     */
	public static void delFile(String fileName){
		File file = new File(SDPATH + fileName);
		if(file.isFile()){
			file.delete();
        }
		file.exists();
	}

	/**
	 * 删除文件夹
	 */
	public static void deleteDir() {
		File dir = new File(SDPATH);
		if (dir == null || !dir.exists() || !dir.isDirectory()) {
            return;
        }
		
		for (File file : dir.listFiles()) {
			if (file.isFile()) {
                file.delete();
            } else if (file.isDirectory()) {
                deleteDir();
            }
		}
		dir.delete();
	}



	/**
	 * 清空文件夹内容
	 * @param root
     */
	public static void deleteAllFiles(File root) {
        File[] files = root.listFiles();
		if (files != null) {
            for (File f : files) {
                if (f.isDirectory()) { // 判断是否为文件夹
                    deleteAllFiles(f);
                    try {
                        f.delete();
                    } catch (Exception e) {
                    }
                } else {
                    if (f.exists()) { // 判断是否存在
                        deleteAllFiles(f);
                        try {
                            f.delete();
                        } catch (Exception e) {
                        }
                    }
                }
            }
        }
	}


	public static String getDataSize(long var0) {
		DecimalFormat var2 = new DecimalFormat("###.00");
		return var0 < 0L?"error":(var0 < 1024L?var0 + "bytes":(var0 < 1048576L?var2.format((double)((float)var0 / 1024.0F)) + "KB":(var0 < 1073741824L?var2.format((double)((float)var0 / 1024.0F / 1024.0F)) + "MB":var2.format((double)((float)var0 / 1024.0F / 1024.0F / 1024.0F)) + "GB")));
	}
}
